package com.doublethread.easymart.modules.order.controller;

import com.doublethread.easymart.common.context.UserContext;
import com.doublethread.easymart.common.result.PageResult;
import com.doublethread.easymart.common.result.Result;
import com.doublethread.easymart.modules.order.dto.OrderCancelDTO;
import com.doublethread.easymart.modules.order.dto.OrderCreateDTO;
import com.doublethread.easymart.modules.order.dto.OrderQueryDTO;
import com.doublethread.easymart.modules.order.service.OrderService;
import com.doublethread.easymart.modules.order.vo.OrderVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户端订单控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/user/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping
    public Result<OrderVO> createOrder(@Valid @RequestBody OrderCreateDTO orderCreateDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("创建订单");

        OrderVO orderVO = orderService.createOrder(userId, orderCreateDTO);

        log.info("订单创建成功");
        return Result.success(orderVO);
    }

    /**
     * 获取用户订单列表
     */
    @GetMapping
    public Result<PageResult<OrderVO>> getUserOrderList(OrderQueryDTO queryDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("获取用户订单列表");

        PageResult<OrderVO> pageResult = orderService.getUserOrderList(userId, queryDTO);

        log.info("获取用户订单列表成功");
        return Result.success(pageResult);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{id}")
    public Result<OrderVO> getOrderDetail(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUserId();

        log.info("获取订单详情");

        OrderVO orderVO = orderService.getOrderById(userId, id);

        log.info("获取订单详情成功");
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     */
    @PutMapping("/{id}/cancel")
    public Result<Void> cancelOrder(@PathVariable Long id,
                                    @Valid @RequestBody OrderCancelDTO cancelDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("取消订单");

        orderService.cancelOrder(userId, id, cancelDTO);

        log.info("订单取消成功");
        return Result.success();
    }

    /**
     * 确认收货
     */
    @PutMapping("/{id}/receive")
    public Result<Void> confirmReceive(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUserId();

        log.info("确认收货");

        orderService.confirmReceive(userId, id);

        log.info("确认收货成功");
        return Result.success();
    }

    /**
     * 支付订单
     */
    @PutMapping("/{id}/pay")
    public Result<Void> payOrder(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUserId();

        log.info("支付订单");

        orderService.payOrder(userId, id);

        log.info("订单支付成功");
        return Result.success();
    }
}
